﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Telerik.Web.UI;

using dotnetCHARTING;
using ChartExtents;

public partial class main_dataQuery_DTZR : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        RadWindowManager1.OffsetElementID = LinkButton1.ClientID;
        RadWindowManager1.Windows[0].OffsetElementID = LinkButton1.ClientID;
        RadWindowManager1.Windows[0].OpenerElementID = LinkButton1.ClientID;
        
        if (!IsPostBack)
        {
            Label_LineName.Text = "练塘-漕泾 >> ";
            Label_TowerName.Text = "C1+1";
            HiddenField_LineName.Value = "练塘-漕泾";
            HiddenField_TowerName.Value = "C1+1";
            RadGrid1.Visible = false;
            LinkButton4.Visible = false;
            Chart1.Visible = false;
            Chart2.Visible = false;
            Chart3.Visible = false;
            //RadChart1.Visible = false;
            LineDAOs lineDAOs = LineDAOs.getInstance();
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            List<String> lineNameList = lineDAOs.getLineNameList();
            for (int i = 0; i < lineNameList.Count; i++)
            {
                RadTreeNode lnode = new RadTreeNode(lineNameList[i]);
                List<String> towerNameList = towerDAOs.getcorrespondingTowerNameList_DTZR(lineNameList[i]);
                for (int j = 0; j < towerNameList.Count; j++)
                {
                    RadTreeNode tnode = new RadTreeNode(towerNameList[j]);
                    lnode.Nodes.Add(tnode);
                }
                RadTreeView1.Nodes.Add(lnode);
            }

        }
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //RadTreeView1.Nodes.Clear();
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        UpdateGridView();

        Label_LineName.Text = HiddenField_LineName.Value + " >> ";
        Label_TowerName.Text = HiddenField_TowerName.Value;
        if (RadGrid1.Visible == false)
            RadGrid1.Visible = true;
        if (Chart1.Visible == true)
            Chart1.Visible = false;
        if (Chart2.Visible == true)
            Chart2.Visible = false;
        if (Chart3.Visible == true)
            Chart3.Visible = false;
        if (LinkButton4.Visible == false)
            LinkButton4.Visible = true;
    }

    protected void RadGrid1_PageIndexChanged(object source, GridPageChangedEventArgs e)
    {   
        RadGrid1.CurrentPageIndex = e.NewPageIndex;
        UpdateGridView();
    }
    protected void RadGrid1_PageSizeChanged(object source, GridPageSizeChangedEventArgs e)
    {
        UpdateGridView();
    }
    //单击显示图形按钮时发生
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            Drawing(i);
        }
        if (RadGrid1.Visible == true)
            RadGrid1.Visible = false;
        if (LinkButton4.Visible == true)
            LinkButton4.Visible = false;
    }

    protected void UpdateGridView()
    {
        DataTable dataTable;
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        dataTable = towerDAOs.getDTDataQueryDTZR(HiddenField_LineName.Value, HiddenField_TowerName.Value, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //this.ViewState["vsTable"] = dataTable;
        RadGrid1.DataSource = dataTable;
        RadGrid1.DataBind();
    }

    //标出告警的单元格
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            for (int i = 0; i < RadGrid1.Columns.Count; i++)
            {
                if (RadGrid1.Columns[i].UniqueName.EndsWith("ABNORMAL"))
                {
                    if (Int64.Parse(item[RadGrid1.Columns[i].UniqueName].Text) > 0)
                    {
                        String s = RadGrid1.Columns[i].UniqueName.Substring(0, RadGrid1.Columns[i].UniqueName.Length - 9);
                        ((GridDataItem)e.Item)[s].ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        if (RadGrid1.Visible == true)
        {
            RadGrid1.ExportSettings.ExportOnlyData = false;
            RadGrid1.ExportSettings.IgnorePaging = true;

            UpdateGridView();
            for (int i = 0; i < RadGrid1.Columns.Count; i++)
            {
                if (RadGrid1.Columns[i].UniqueName.EndsWith("ABNORMAL"))
                {
                    RadGrid1.MasterTableView.GetColumn(RadGrid1.Columns[i].UniqueName).Visible = false;
                }
            }
            RadGrid1.MasterTableView.ExportToCSV();
        }
    }
    private void Drawing(int i)
    {
        Charting c = new Charting();
        Chart[] chart = { this.Chart1, this.Chart2 ,this.Chart3};
        c.Title = "";
        c.XTitle = "时间";
        if (i == 0)
        {
            c.YTitle = "温度/℃";
            c.DataSource = GetDataSource();
        }
        else if (i == 1)
        {
            c.YTitle = "电流/A";
            c.DataSource = GetDataSourceB();
        }
        else
        {
            c.YTitle = "功率/MW";
            c.DataSource = GetDataSourceC();
        }
        c.PicHight = 280;
        c.PicWidth = 550;
        //c.SeriesName = "合计";//仅对于DataTable类型做数据源时，此属性有效
        c.PhaysicalImagePath = "images";//统计图片存放的文件夹名称，缺少对应的文件夹生成不了统计图片
        c.FileName = "Statistics" + i.ToString();
        c.Type = SeriesType.Spline;
        c.Use3D = false;
        c.CreateStatisticPic(chart[i]);
    }

    private SeriesCollection GetDataSource()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();

        DataSet ds = towerDAOs.getSpine(HiddenField_LineName.Value, HiddenField_TowerName.Value, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value, "temperature", "temperatureof_line");
        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            // 生成对比图
            for (int a = 1; a <= 2; a++) //对比的项数
            {
                Series s = new Series();
                s.Name = (a == 1 ? "环境温度" : "导线温度");//各个数据项代表的名称
                for (int b = 0; b < 10; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    if (s.Name == "环境温度")
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["temperature"].ToString());
                    }
                    else
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["temperatureof_line"].ToString());
                    }
                    s.Elements.Add(e);
                }
                SC.Add(s);
            }
            this.Chart1.Visible = true;
        }
        //可自定义填充图的填充色，系统采取默认分配各数据项的填充色
        //SC[0].DefaultElement.Color = Color.Blue;
        //SC[1].DefaultElement.Color = Color.Red;
        //SC[2].DefaultElement.Color = Color.FromArgb(255, 99, 49);
        //SC[3].DefaultElement.Color = Color.FromArgb(0, 156, 255);
        return SC;
    }
    private SeriesCollection GetDataSourceB()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();

        DataSet ds = towerDAOs.getSpine(HiddenField_LineName.Value, HiddenField_TowerName.Value, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value, "current_realtime", "max_current");
        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            // 生成对比图
            for (int a = 1; a <= 2; a++) //对比的项数
            {
                Series s = new Series();
                s.Name = (a == 1 ? "实时电流" : "限额电流");//各个数据项代表的名称
                for (int b = 0; b < 10; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    if (s.Name == "实时电流")
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["current_realtime"].ToString());
                    }
                    else
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["max_current"].ToString());
                    }
                    s.Elements.Add(e);
                }
                SC.Add(s);
            }
            this.Chart2.Visible = true;
        }
        //可自定义填充图的填充色，系统采取默认分配各数据项的填充色
        //SC[0].DefaultElement.Color = Color.Blue;
        //SC[1].DefaultElement.Color = Color.Red;
        //SC[2].DefaultElement.Color = Color.FromArgb(255, 99, 49);
        //SC[3].DefaultElement.Color = Color.FromArgb(0, 156, 255);
        return SC;
    }
    private SeriesCollection GetDataSourceC()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds = towerDAOs.getSpine(HiddenField_LineName.Value, HiddenField_TowerName.Value, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value, "limitof_power", "threholdof_heat");
        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            // 生成对比图
            for (int a = 1; a <= 2; a++) //对比的项数
            {
                Series s = new Series();
                s.Name = (a == 1 ? "实时功率" : "热稳限额");//各个数据项代表的名称
                for (int b = 0; b < 10; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    if (s.Name == "实时功率")
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["limitof_power"].ToString());
                    }
                    else
                    {
                        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["threholdof_heat"].ToString());
                    }
                    s.Elements.Add(e);
                }
                SC.Add(s);
            }
            this.Chart3.Visible = true;
        }
        //可自定义填充图的填充色，系统采取默认分配各数据项的填充色
        //SC[0].DefaultElement.Color = Color.Blue;
        //SC[1].DefaultElement.Color = Color.Red;
        //SC[2].DefaultElement.Color = Color.FromArgb(255, 99, 49);
        //SC[3].DefaultElement.Color = Color.FromArgb(0, 156, 255);
        return SC;
    }
}
